package com.ultrawise.survey.module.surevybase.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.common.utils.StringUtils;

import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.surevybase.service.TSurveyDataAnswerService;
import com.ultrawise.survey.module.surevybase.service.TSurveyDataTotalService;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoQuestionService;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoQuestion;

@Controller
@RequestMapping("/symanage/total")
public class TSurveyDataTotalControl extends BaseContoller {

	@Autowired
	TSurveyDataTotalService tSurveyDataTotalService;
	@Autowired
	TSurveyInfoQuestionService tSurveyInfoQuestionService;
	
	@Autowired
	TSurveyDataAnswerService tSurveyDataAnswerService;
	
	
	@RequestMapping(value = "total1.htm")
	public ModelAndView total1(HttpServletRequest request) throws ServiceException {
		String syid = request.getParameter("syid");
		Integer pse = tSurveyDataTotalService.queryPersonnum(syid);
		request.setAttribute("syid", syid);
		request.setAttribute("pse", pse);
		//数据库的准备阶段及SQl的查询
		List<TSurveyInfoQuestion> qlist= tSurveyInfoQuestionService.query("sy_id", syid, new TSurveyInfoQuestion(), TSurveyInfoQuestion.class);
		request.setAttribute("qlist", qlist);
		//数据库的准备阶段及SQl的查询
		List<Map<String,Object>> ch=tSurveyDataAnswerService.queryPersonnuByquestion(syid);
		
		//返回每个问题的选项的数量。
		Map<String,Integer> opnum = getQutionOptionNumBySyid(syid);
		
		//匹配问题的选项的数量
		Map<Integer,Map<String,Integer>> chnum=new HashMap<Integer,Map<String,Integer>>();
		for(Map<String,Object> cha:ch){
			Map<String,Integer> chanum = new HashMap<String,Integer>();
			String aschoose=(String)cha.get("AS_CHOOSE");
			Integer optionnum = opnum.get(cha.get("QN_ID").toString());
			//循环选项内容，匹配具体的选择的数量信息
			for(int i=0;optionnum!=null && i<optionnum;i++){
				String optemp = "ob"+(i+1);
				Integer includnum = StringUtils.getStringIncludeNum(aschoose, optemp);
				chanum.put(optemp, includnum);
			}
			//chanum.put(key, value);			
			chnum.put((Integer)cha.get("QN_ID"), chanum);
			
		}
		request.setAttribute("chnum", chnum);
		return new ModelAndView("/total/total1");
	}
	
	
	/****
	 *  返回问题的选线数量，只是统计类型不为问答题的内容。选择，多选择
	 * @param syid
	 * @return
	 */
	private Map<String,Integer> getQutionOptionNumBySyid(String syid){
		List<Map<String,Object>> quoption =tSurveyInfoQuestionService.getOptionsNumBySyid(syid);
		Map<String,Integer> opnum = new HashMap<String,Integer>();
		for(Map<String,Object> opt:quoption){
			Long tpnum=(Long)opt.get("OPTIONNUM");
			Integer opn = Integer.parseInt(tpnum.toString());
			opnum.put(opt.get("QN_ID").toString(),opn);
		}
		return opnum;
	}
	

	@RequestMapping(value = "total2.htm")
	public ModelAndView total2(HttpServletRequest request) throws ServiceException {
		String syid = request.getParameter("syid");
		String qnid = request.getParameter("qnid");
		List<Map<String,Object>> ls=tSurveyDataTotalService.queryinfo(syid,qnid);
		request.setAttribute("qls", ls);
		return new ModelAndView("/total/total2");
}
}

